package com.tanquandan.askbackend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanquandan.askbackend.dao.EmployeeMapper;
import com.tanquandan.askbackend.dto.R;
import com.tanquandan.askbackend.entity.Employee;
import com.tanquandan.askbackend.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;

@Service
public class EmployeeService extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public R<Employee> login(HttpServletRequest request, Employee employee) {
        // 1. 查找用户是否存在
        String loginName = employee.getUsername();
        Employee checkEmloyee = query().eq("username", loginName).one();
        if(checkEmloyee == null) return R.error("用户未进行注册!");

        // 2. 若存在，则用用户输入密码比对数据库中的密码
        String loginPassword = employee.getPassword();
        if(!loginPassword.equals(checkEmloyee.getPassword())) return R.error("密码错误!");

        // 3. 如果密码比对成功，检查用户是否被禁用
        if(checkEmloyee.getStatus() == 0)  return R.error("用户被禁用!");

        // 4. 如果没有被禁用，则说明用户登录成功。将用户存储到session中
        request.getSession().setAttribute("employee",checkEmloyee.getId());

        // 5. 返回登录用户
        return R.success(checkEmloyee);
    }
}
